package org.gwtde.client.views;

import net.mygwt.ui.client.Registry;
import net.mygwt.ui.client.Style;
import net.mygwt.ui.client.event.BaseEvent;
import net.mygwt.ui.client.event.SelectionListener;
import net.mygwt.ui.client.mvc.AppEvent;
import net.mygwt.ui.client.mvc.Controller;
import net.mygwt.ui.client.mvc.Dispatcher;
import net.mygwt.ui.client.mvc.View;
import net.mygwt.ui.client.widget.Info;
import net.mygwt.ui.client.widget.Shell;
import net.mygwt.ui.client.widget.ShellManager;
import net.mygwt.ui.client.widget.ToolBar;
import net.mygwt.ui.client.widget.ToolItem;
import net.mygwt.ui.client.widget.Viewport;
import net.mygwt.ui.client.widget.WidgetContainer;
import net.mygwt.ui.client.widget.layout.RowData;
import net.mygwt.ui.client.widget.layout.RowLayout;
import net.mygwt.ui.client.widget.menu.Menu;
import net.mygwt.ui.client.widget.menu.MenuItem;

import org.gwtde.client.Constants;
import org.gwtde.client.DesktopEvents;

public class DesktopView extends View {

	private Viewport mViewport;

	private ToolBar mToolBar = new ToolBar();

	public DesktopView(Controller pController) {
		super(pController);
	}

	protected void initialize() {
		mViewport = new Viewport();
		mViewport.setStyleName("my-border-layout");
		mViewport.setLayout(new RowLayout(Style.VERTICAL));

		mViewport.add(mToolBar, new RowData(RowData.FILL_HORIZONTAL));

		Menu dockMenu = new Menu();

		MenuItem menuItem = new MenuItem(Style.PUSH);
		menuItem.setText("Preferences...");
		menuItem.addSelectionListener(new SelectionListener() {
			public void widgetSelected(BaseEvent be) {
				Info.show("Preferences...", "{0}", "Not implemented!");
			}
		});
		dockMenu.add(menuItem);

		menuItem = new MenuItem(Style.PUSH);
		menuItem.setText("About...");
		menuItem.setIconStyle("icon-info");
		menuItem.addSelectionListener(new SelectionListener() {
			public void widgetSelected(BaseEvent be) {
				Info.show("About...", "{0}", "Not implemented!");
			}
		});
		dockMenu.add(menuItem);

		ToolItem item = new ToolItem(Style.SPLIT_MENU);
		item.setText("<b>hi!</b>");
		item.setIconStyle("icon-face-smile");
		item.setMenu(dockMenu);
		item.addSelectionListener(new SelectionListener() {
			public void widgetSelected(BaseEvent be) {
				Dispatcher.forwardEvent(DesktopEvents.DockActivate);
			}
		});
		mToolBar.add(item);

		mToolBar.add(new ToolItem(Style.SEPARATOR));

		WidgetContainer c = new WidgetContainer();
		mViewport.add(c, new RowData(RowData.FILL_BOTH));

		mViewport.layout();
		mViewport.hideLoadingPanel("loading");

		Registry.register("viewport", mViewport);
		Registry.register(Constants.DESKTOP_CONTAINER, c);
	}

	protected void handleEvent(AppEvent event) {
		if (event.type == DesktopEvents.Init) {
			// ExpandItem item = expandBar.getItem(0);
			// expandBar.setExpanded(item, true);
		}
	}

	public ToolItem add(final Shell pShell) {
		ToolItem item = new ToolItem(Style.TOGGLE);
		item.setText(pShell.getText());
		item.setIconStyle("icon-shell");
		item.addSelectionListener(new SelectionListener() {
			public void widgetSelected(BaseEvent be) {
				if (pShell.isVisible() && ShellManager.get().isActive(pShell)) {
					pShell.hide();
					((ToolItem)be.item).setSelected(false); // FIXME
				} else if (pShell.isVisible()) {
					ShellManager.get().setActive(pShell);
				} else {
					pShell.show();
					ShellManager.get().setActive(pShell);
					((ToolItem)be.item).setSelected(true);	// FIXME
				}
			}
		});
		mToolBar.add(item);
		return item;
	}

	public void remove(ToolItem pItem) {
		mToolBar.remove(pItem);
	}

}
